/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inovacao.gui;

import inovacao.entidades.Cidade;
import inovacao.interfaces.MasterForm;
import inovacao.modelos.CustomizadoTextField;
import inovacao.modelos.PagingModel;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Giovani
 */
public class GridDetalhe extends JTable {

    private MasterForm masterForm;
    private PagingModel pm;
    private Navegadores navegadores;

    public Navegadores getNavegadores() {
        return navegadores;
    }

    public void setNavegadores(Navegadores navegadores) {
        this.navegadores = navegadores;
    }

    public PagingModel getPm() {
        return pm;
    }

    public void setPm(PagingModel pm) {
        this.pm = pm;
    }

    public MasterForm getMasterForm() {
        return masterForm;
    }

    public void setMasterForm(MasterForm masterForm) {
        this.masterForm = masterForm;
    }

    public void selectPrimeiraLinha() {
        if (getSelectedRow() < getRowCount() - 1) {
            setColumnSelectionInterval(0, getColumnCount() - 1);
            setRowSelectionInterval(getSelectedRow() + 1, getSelectedRow() + 1);
        }
    }

    public void verificaAlteracao() {
        if (this.pm.isEditavel()) {
            this.pm.setEditavel(false);
            this.navegadores.desabilitaHabilitaBotoes(1, 1, 0, 0, 1, 1);

            ArrayList<Component> componentes = this.masterForm.allComponentes();
            for (Component componente : componentes) {
                componente.setEnabled(false);
            }

        }
    }

    public GridDetalhe(PagingModel pm) {
        super(pm);
        this.pm = pm;
        setCellSelectionEnabled(false);
        setRowSelectionAllowed(true);

        this.setCellSelectionEnabled(true);
        ListSelectionModel cellSelectionModel = this.getSelectionModel();
        cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        if (pm.isListenerCamposTela()) {
            cellSelectionModel.addListSelectionListener(new ListSelectionListener() {

                public void valueChanged(ListSelectionEvent e) {

                    verificaAlteracao();
                    String selectedData = null;


                    setColumnSelectionInterval(0, getColumnCount() - 1);
                    int[] selectedRow = getSelectedRows();
                    int[] selectedColumns = getSelectedColumns();

                    for (int i = 0; i < selectedRow.length; i++) {
                        for (int j = 0; j < selectedColumns.length; j++) {

                            ArrayList<Component> componentes = masterForm.allComponentes();

                            if (componentes.get(j) instanceof CustomizadoTextField) {
                                selectedData = (String) getValueAt(selectedRow[i], selectedColumns[j]).toString();
                                ((JTextField) componentes.get(j)).setText(selectedData);
                            }
                            if (componentes.get(j) instanceof JComboBox) {
                                if ((getValueAt(selectedRow[i], selectedColumns[j])) instanceof Cidade) {
                                    Cidade cidade = (Cidade) (getValueAt(selectedRow[i], selectedColumns[j]));
                                    ((JComboBox) componentes.get(j)).setSelectedItem(cidade);
                                }
                            }
                        }
                    }
                    //JOptionPane.showMessageDialog(null, "Selected: o " + selectedData);
                }
            });
        }
    }

    public GridDetalhe(Vector<Object> um, Vector<Object> dois) {
        super(um, dois);
        this.setCellSelectionEnabled(true);
        ListSelectionModel cellSelectionModel = this.getSelectionModel();
        cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        cellSelectionModel.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                String selectedData = null;

                int[] selectedRow = getSelectedRows();
                int[] selectedColumns = {0};//getSelectedColumns();

                for (int i = 0; i < selectedRow.length; i++) {
                    for (int j = 0; j < selectedColumns.length; j++) {
                        selectedData = (String) getValueAt(selectedRow[i], selectedColumns[j]);
                    }
                }
                JOptionPane.showMessageDialog(null, "Selected: " + selectedData);
            }
        });
    }

    public void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
        if (!(table.getParent() instanceof JViewport)) {
            return;
        }
        JViewport viewport = (JViewport) table.getParent();
        Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
        Point pt = viewport.getViewPosition();
        rect.setLocation(rect.x - pt.x, rect.y - pt.y);
        viewport.scrollRectToVisible(rect);
    }
}
